const db = require('../db/index')
/**
 * 获取文章分类列表函数
 * 
 * @param {*} req 
 * @param {*} res 
 */
exports.getArticleCates = (req, res) => {
    const sql = 'select * from article where is_delete=0 order by id asc'
    db.query(sql, (err, result) => {
        if (err) {
            return req.cc(err)
        }
        res.send({
            status: 200,
            message: '成功',
            data: result
        })
    })
}
/**
 * 新增文章函数
 * 
 * @param {*} req 
 * @param {*} res 
 */
exports.addArticleCates = (req, res) => {
    const sql = `select * from article where name='${req.body.name}' or alias='${req.body.alias}'`
    db.query(sql, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        const length = result.length
        if (length == 2) {
            return res.cc('名称和别名都被占用')
        }
        if (length == 1) {
            const article = result[0]
            if (article.name == req.body.name && article.alias == req.body.alias) {
                return res.cc('名称和别名都被占用')
            }
            if (article.name == req.body.name && article.alias !== req.body.alias) {
                return res.cc('名称被占用')
            }
            if (article.alias == req.body.alias && article.name !== req.body.name) {
                return res.cc('别名被占用')
            }
        }
        const insertSql = 'insert into article set ?'
        db.query(insertSql, {
            name: req.body.name,
            alias: req.body.alias,
            is_delete: 0
        }, function (err, result) {
            if (err) {
                return res.cc(err)
            }
            return res.cc('插入成功')
        })

    })
}
/**
 * 删除文章函数
 * 
 * @param {*} req 
 * @param {*} res 
 */
exports.deleteArticle = (req, res) => {
    const sql = `update article set is_delete=1  where id =${req.params.id}`
    db.query(sql, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        return res.cc('删除成功')
    })
}
/**
 * 通过id获取文章
 * 
 * @param {*} req 
 * @param {*} res 
 */

exports.getArticleById = (req, res) => {
    const sql = `select * from article where id=${req.params.id} and is_delete=0`
    db.query(sql, function (err, result) {
        if (err) {
            return res.cc(err)
        }
        return res.cc(result.length > 0 ? result[0] : {})
    })
}
/**
 * 根据id更新文章函数
 * 
 * @param {*} req 
 * @param {*} res 
 */
exports.updateArticleById = (req, res) => {
    const sql = 'select * from article where id <>? and (name=? or alias=?)'
    db.query(sql, [req.body.id, req.body.name, req.body.alias], (err, result) => {
        if (err) {
            return res.cc(err)
        }
        const length = result.length
        if (length == 2) {
            return res.cc('名称和别名都被占用')
        }
        if (length == 1) {
            const article = result[0]
            if (article.name == req.body.name && article.alias == req.body.alias) {
                return res.cc('名称和别名都被占用')
            }
            if (article.name == req.body.name && article.alias !== req.body.alias) {
                return res.cc('名称被占用')
            }
            if (article.alias == req.body.alias && article.name !== req.body.name) {
                return res.cc('别名被占用')
            }
        }
        const updateSql = 'update article set name=?,alias=? where id=?'
        db.query(updateSql, [req.body.name, req.body.alias, req.body.id], function (err, result) {
            if (err) {
                return res.cc(err)
            }
            return res.cc('更新成功')
        })
    })
}